#!/usr/bin/env python3
"""
使用示例 - MCP Greeting & Calculator Server
展示如何与MCP服务器进行交互的示例代码
"""

import asyncio
import json
import sys
import os

# 添加当前目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from server import greet, farewell, add, subtract, multiply, divide, square, sqrt, set_preferences

def greeting_examples():
    """打招呼功能示例"""
    print("🎉 打招呼功能示例")
    print("=" * 50)
    
    # 示例1：基本问候
    print("1. 基本问候：")
    result = greet(name="张三")
    print(f"   输入: {{\"name\": \"张三\"}}")
    print(f"   输出: {result}\n")
    
    # 示例2：正式风格英文问候
    print("2. 正式风格英文问候：")
    result = greet(
        name="Mr. Smith", 
        style="formal", 
        language="en"
    )
    print(f"   输入: {{\"name\": \"Mr. Smith\", \"style\": \"formal\", \"language\": \"en\"}}")
    print(f"   输出: {result}\n")
    
    # 示例3：随意风格问候
    print("3. 随意风格问候：")
    result = greet(
        name="小李", 
        style="casual", 
        language="zh"
    )
    print(f"   输入: {{\"name\": \"小李\", \"style\": \"casual\", \"language\": \"zh\"}}")
    print(f"   输出: {result}\n")
    
    # 示例4：告别消息
    print("4. 告别消息：")
    result = farewell(
        name="朋友", 
        style="friendly", 
        language="zh"
    )
    print(f"   输入: {{\"name\": \"朋友\", \"style\": \"friendly\", \"language\": \"zh\"}}")
    print(f"   输出: {result}\n")

def calculator_examples():
    """计算器功能示例"""
    print("🧮 计算器功能示例")
    print("=" * 50)
    
    # 基本运算示例
    operations = [
        (add, {"a": 15, "b": 25}, "加法"),
        (subtract, {"a": 50, "b": 18}, "减法"),
        (multiply, {"a": 8, "b": 9}, "乘法"),
        (divide, {"a": 84, "b": 12}, "除法"),
        (square, {"number": 12}, "平方"),
        (sqrt, {"number": 144}, "开根号")
    ]
    
    for i, (op_func, params, desc) in enumerate(operations, 1):
        result = op_func(**params)
        print(f"{i}. {desc}：")
        print(f"   输入: {json.dumps(params, ensure_ascii=False)}")
        print(f"   输出: {result}\n")
    
    # 错误处理示例
    print("错误处理示例：")
    print("7. 除零错误：")
    result = divide(a=10, b=0)
    print(f"   输入: {{\"a\": 10, \"b\": 0}}")
    print(f"   输出: {result}\n")
    
    print("8. 负数开根号错误：")
    result = sqrt(number=-9)
    print(f"   输入: {{\"number\": -9}}")
    print(f"   输出: {result}\n")

def preferences_examples():
    """偏好设置示例"""
    print("⚙️ 偏好设置示例")
    print("=" * 50)
    
    # 设置用户偏好
    print("1. 设置用户偏好：")
    result = set_preferences(
        name="王小明",
        greeting_style="casual",
        language="zh"
    )
    print(f"   输入: {{\"name\": \"王小明\", \"greeting_style\": \"casual\", \"language\": \"zh\"}}")
    print(f"   输出: {result}\n")
    
    # 使用默认偏好
    print("2. 使用默认偏好问候：")
    result = greet()
    print(f"   输入: {{}}")
    print(f"   输出: {result}\n")
    
    # 覆盖默认偏好
    print("3. 临时覆盖默认偏好：")
    result = greet(style="formal")
    print(f"   输入: {{\"style\": \"formal\"}}")
    print(f"   输出: {result}\n")

def complex_scenarios():
    """复杂使用场景示例"""
    print("🚀 复杂使用场景示例")
    print("=" * 50)
    
    # 场景1：数学课堂
    print("场景1：数学课堂助手")
    print("-" * 30)
    
    # 问候学生
    result = greet(
        name="同学们", 
        style="friendly", 
        language="zh"
    )
    print(f"老师: {result}")
    
    # 进行数学计算
    calculations = [
        (square, {"number": 5}, "5的平方是多少？"),
        (sqrt, {"number": 25}, "25的平方根是多少？"),
        (multiply, {"a": 12, "b": 8}, "12乘以8等于多少？")
    ]
    
    for question, (op_func, params, _) in zip(["问题1", "问题2", "问题3"], calculations):
        result = op_func(**params)
        print(f"{question}: {_}")
        print(f"答案: {result}")
    
    # 课程结束
    result = farewell(
        name="同学们", 
        style="friendly", 
        language="zh"
    )
    print(f"老师: {result}\n")
    
    # 场景2：国际商务会议
    print("场景2：国际商务会议")
    print("-" * 30)
    
    # 正式问候
    result = greet(
        name="Johnson", 
        style="formal", 
        language="en"
    )
    print(f"Host: {result}")
    
    # 计算业务数据
    result = add(a=125000, b=87500)
    print(f"Q1 + Q2 Revenue: {result}")
    
    result = divide(a=212500, b=2)
    print(f"Average Quarterly Revenue: {result}")
    
    # 正式告别
    result = farewell(
        name="Johnson", 
        style="formal", 
        language="en"
    )
    print(f"Host: {result}\n")

def main():
    """主函数"""
    print("MCP Greeting & Calculator Server - 使用示例")
    print("=" * 60)
    print()
    
    greeting_examples()
    calculator_examples()
    preferences_examples()
    complex_scenarios()
    
    print("✅ 所有示例演示完成！")
    print("\n💡 提示：")
    print("- 你可以根据这些示例来使用MCP服务器")
    print("- 所有功能都支持个性化定制")
    print("- 服务器会自动处理错误情况")
    print("- 支持中英文双语交互")

if __name__ == "__main__":
    main()